#include "kernel/shared-memory.h"
#include "kernel-c/kernel.h"
#include "kernel-cpp/kernel-class.h"

#include <cstdlib>
#include <ctime>
#include <memory>


SharedData* shared_memory_sys{};

KSError __stdcall timerCallBack(void* /*pArgs*/, void* /*pContext*/);

extern "C" KSError _declspec(dllexport) __stdcall _initFunction(void* argc, void* content) {
    shared_memory_sys = (SharedData*)argc;
    KernelClass kernel_class{};
    shared_memory_sys->number = kernel_class.getNumber();
    auto ksError = KS_createCallBack(
        &shared_memory_sys->hTimerCallBack,
        timerCallBack,
        NULL,
        KSF_DIRECT_EXEC,
        0);
    if (ksError != KS_OK) {
        return ksError;
    }
    ksError = KS_createTimer(
        &shared_memory_sys->hTimer,
        500 * ms,
        shared_memory_sys->hTimerCallBack,
        KSF_REALTIME_EXEC);
    if (ksError != KS_OK)
        return ksError;

    KS_map<int,KS_string> map_test;
    map_test.insert(std::make_pair(1, "one"));

    return KS_OK;
}

extern "C" KSError _declspec(dllexport) __stdcall _exitFunction(void* argc, void* content) {
    GetNumber(shared_memory_sys->number);
    auto ksError = KS_removeTimer(shared_memory_sys->hTimer);
    if (ksError != KS_OK) {
        return ksError;
    }
    ksError = KS_removeCallBack(shared_memory_sys->hTimerCallBack);
    if (ksError != KS_OK) {
        return ksError;
    }
    return KS_OK;
}

KSError __stdcall timerCallBack(void* /*pArgs*/, void* /*pContext*/) {
    // 消息管道压数据
    static int index{};
    KS_vector<KS_string> message_vector;
    message_vector.push_back("          _____                    _____                _____                    _____          \n");
    message_vector.push_back("         /\\    \\                  /\\    \\              /\\    \\                  /\\    \\\n");
    message_vector.push_back("        /::\\____\\                /::\\    \\            /::\\    \\                /::\\    \\\n");
    message_vector.push_back("       /:::/    /               /::::\\    \\           \\:::\\    \\              /::::\\    \\\n");
    message_vector.push_back("      /:::/    /               /::::::\\    \\           \\:::\\    \\            /::::::\\    \\\n");
    message_vector.push_back("     /:::/    /               /:::/\\:::\\    \\           \\:::\\    \\          /:::/\\:::\\    \\\n");
    message_vector.push_back("    /:::/____/               /:::/__\\:::\\    \\           \\:::\\    \\        /:::/__\\:::\\    \\\n");
    message_vector.push_back("   /::::\\    \\              /::::\\   \\:::\\    \\          /::::\\    \\       \\:::\\   \\:::\\    \\\n");
    message_vector.push_back("  /::::::\\____\\________    /::::::\\   \\:::\\    \\        /::::::\\    \\    ___\\:::\\   \\:::\\    \\\n");
    message_vector.push_back(" /:::/\\:::::::::::\\    \\  /:::/\\:::\\   \\:::\\____\\      /:::/\\:::\\    \\  /\\   \\:::\\   \\:::\\    \\\n");
    message_vector.push_back("/:::/  |:::::::::::\\____\\/:::/  \\:::\\   \\:::|    |    /:::/  \\:::\\____\\/::\\   \\:::\\   \\:::\\____\\\n");
    message_vector.push_back("\\::/   |::|~~~|~~~~~     \\::/   |::::\\  /:::|____|   /:::/    \\::/    /\\:::\\   \\:::\\   \\::/    /\n");
    message_vector.push_back(" \\/____|::|   |           \\/____|:::::\\/:::/    /   /:::/    / \\/____/  \\:::\\   \\:::\\   \\/____/\n");
    message_vector.push_back("       |::|   |                 |:::::::::/    /   /:::/    /            \\:::\\   \\:::\\    \\\n");
    message_vector.push_back("       |::|   |                 |::|\\::::/    /   /:::/    /              \\:::\\   \\:::\\____\\\n");
    message_vector.push_back("       |::|   |                 |::| \\::/____/    \\::/    /                \\:::\\  /:::/    /\n");
    message_vector.push_back("       |::|   |                 |::|  ~|           \\/____/                  \\:::\\/:::/    /\n");
    message_vector.push_back("       |::|   |                 |::|   |                                     \\::::::/    /\n");
    message_vector.push_back("       \\::|   |                 \\::|   |                                      \\::::/    /\n");
    message_vector.push_back("        \\:|   |                  \\:|   |                                       \\::/    /\n");
    message_vector.push_back("         \\|___|                   \\|___|                                        \\/____/\n");
    message_vector.push_back("                                                                                                \n");
    message_vector.push_back("                                                                                                \n");
    message_vector.push_back("                                                                                                \n");
    message_vector.push_back("KKKKKKKKK    KKKKKKKRRRRRRRRRRRRRRRRR   TTTTTTTTTTTTTTTTTTTTTTT   SSSSSSSSSSSSSSS\n");
    message_vector.push_back("K:::::::K    K:::::KR::::::::::::::::R  T:::::::::::::::::::::T SS:::::::::::::::S\n");
    message_vector.push_back("K:::::::K    K:::::KR::::::RRRRRR:::::R T:::::::::::::::::::::TS:::::SSSSSS::::::S\n");
    message_vector.push_back("K:::::::K   K::::::KRR:::::R     R:::::RT:::::TT:::::::TT:::::TS:::::S     SSSSSSS\n");
    message_vector.push_back("KK::::::K  K:::::KKK  R::::R     R:::::RTTTTTT  T:::::T  TTTTTTS:::::S\n");
    message_vector.push_back("  K:::::K K:::::K     R::::R     R:::::R        T:::::T        S:::::S\n");
    message_vector.push_back("  K::::::K:::::K      R::::RRRRRR:::::R         T:::::T         S::::SSSS\n");
    message_vector.push_back("  K:::::::::::K       R:::::::::::::RR          T:::::T          SS::::::SSSSS\n");
    message_vector.push_back("  K:::::::::::K       R::::RRRRRR:::::R         T:::::T            SSS::::::::SS\n");
    message_vector.push_back("  K::::::K:::::K      R::::R     R:::::R        T:::::T               SSSSSS::::S\n");
    message_vector.push_back("  K:::::K K:::::K     R::::R     R:::::R        T:::::T                    S:::::S\n");
    message_vector.push_back("KK::::::K  K:::::KKK  R::::R     R:::::R        T:::::T                    S:::::S\n");
    message_vector.push_back("K:::::::K   K::::::KRR:::::R     R:::::R      TT:::::::TT      SSSSSSS     S:::::S\n");
    message_vector.push_back("K:::::::K    K:::::KR::::::R     R:::::R      T:::::::::T      S::::::SSSSSS:::::S\n");
    message_vector.push_back("K:::::::K    K:::::KR::::::R     R:::::R      T:::::::::T      S:::::::::::::::SS\n");
    message_vector.push_back("KKKKKKKKK    KKKKKKKRRRRRRRR     RRRRRRR      TTTTTTTTTTT       SSSSSSSSSSSSSSS\n");
    message_vector.push_back("                                                                                                \n");
    message_vector.push_back("                                                                                                \n");
    message_vector.push_back("                                                                                                \n");

    auto ksError = KS_putPipe(shared_memory_sys->hPipe, message_vector[index].data(),
                              message_vector[index].length(), NULL, KSF_NO_FLAGS);
    (index < message_vector.size() - 1) ? index++ : index = 0;
    if (ksError != KS_OK) {
        return ksError;
    }
    ksError = KS_setEvent(shared_memory_sys->hWaitEvent);
    if (ksError != KS_OK) {
        return ksError;
    }
    return KS_OK;
}

#define WIN32_LEAN_AND_MEAN
#pragma pack(push, 8)
#include <windows.h>
#pragma pack(pop)

BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD reason, LPVOID pReserved) {
  return TRUE;
}

